package com.tarry.io.file;

import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.atomic.AtomicInteger;

/**
 *  快速便利文件夹的文件
 */

@Slf4j
public class FilesWalkFiletTreeDemo {
    public static void main(String[] args) throws IOException {
        AtomicInteger dirCount = new AtomicInteger();
        AtomicInteger fileCount = new AtomicInteger();

        /*
            此处涉及一个访问者模式
         */
        Files.walkFileTree(Paths.get("C:\\Program Files (x86)\\OpenJDK\\jdk-8.0.362.09-hotspot") , new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                // 访问文件夹
                log.info("dir ===> {}", dir);
                dirCount.incrementAndGet();
                return super.preVisitDirectory(dir, attrs);
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                // 访问文件
                log.info("file ===> {}", file);
                fileCount.incrementAndGet();
                return super.visitFile(file, attrs);
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                log.info("退出文件目录: {}", dir);
                return super.postVisitDirectory(dir, exc);
            }
        });
        log.warn("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        log.info("dir is [{}]", dirCount);
        log.info("file is [{}]", fileCount);

    }
}
